package com.sx.pipeline.repos.local

import com.blankj.utilcode.util.CacheDiskUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.google.gson.reflect.TypeToken
import com.sx.mvvmlibs.http.HttpRequest
import com.sx.pipeline.app.config.AppConstants
import com.sx.pipeline.app.config.SPKey
import com.sx.pipeline.repos.http.res.*
import com.sx.pipeline.repos.local.bean.*
import com.sx.pipeline.repos.local.db.AppDB
import com.sx.pipeline.repos.local.db.table.PipeFileBeanDO
import io.reactivex.rxjava3.core.Single
import java.io.Serializable

/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: TODO
 */
object LocalSourceImpl: ILocalSource {

    // 本地数据库
    private val appDB: AppDB = AppDB.getInstance()

    override fun setToken(token: String) {
        SPUtils.getInstance().put(SPKey.SP_TOKEN, token)
        HttpRequest.addDefaultHeader("token", SPUtils.getInstance().getString(SPKey.SP_TOKEN, ""))
    }


    override fun getToken(): String? {
        return SPUtils.getInstance().getString(SPKey.SP_TOKEN, "")
    }

    override fun clearToken() {
        SPUtils.getInstance().remove(SPKey.SP_TOKEN)
    }

    override fun setNoticeUnreadNum(value: Int) {
        SPUtils.getInstance().put(SPKey.SP_NOTICE_UNREAD_NUM, value)
    }

    override fun getNoticeUnreadNum(): Int {
        return SPUtils.getInstance().getInt(SPKey.SP_NOTICE_UNREAD_NUM)
    }

    override fun setPayWord(isset: Boolean) {
        SPUtils.getInstance().put(SPKey.SP_SET_PAY, isset)
    }

    override fun getPayWord(): Boolean? {
        return SPUtils.getInstance().getBoolean(SPKey.SP_SET_PAY)
    }


    override fun saveUserData(userInfo: UserInfoVO) {
        SPUtils.getInstance().put(SPKey.SP_USER_JSON_DATA,
            GsonUtils.toJson(userInfo, object : TypeToken<UserInfoVO>() {}.type))
    }

    override fun getUserData(): UserInfoVO? {
        val userJsonData = SPUtils.getInstance().getString(SPKey.SP_USER_JSON_DATA)
        return if (userJsonData.isBlank())
            return null
        else GsonUtils.fromJson(
            userJsonData,
            object : TypeToken<UserInfoVO>() {}.type
        )
    }

    override fun saveIdentityType(type: Int) {
        SPUtils.getInstance().put(SPKey.SP_IDENTITY_TYPE, type)
    }

    override fun getIdentityType(): Int {
        return SPUtils.getInstance().getInt(SPKey.SP_IDENTITY_TYPE, -1)
    }

    override fun setPermission(value: Boolean) {
        SPUtils.getInstance().put(SPKey.SP_PERMISSION, value)
    }

    override fun <T : Serializable> saveCacheData(obj: T?) {
        if (obj != null) {
            when(obj) {
                is UserInfoVO -> {
                    CacheDiskUtils.getInstance().put(
                        AppConstants.CacheKey.CACHE_USER_INFO_DATA,
                        UserInfoDataCache(obj),
                        AppConstants.CacheKey.CACHE_SAVE_TIME_SECONDS
                    )
                }
            }
        }
    }

    override fun <T : Serializable> getCacheData(key: String): T? {
        return when (val serializable = CacheDiskUtils.getInstance().getSerializable(key)) {
            is UserInfoDataCache? -> {
                serializable?.userInfo as T
            }
            else -> null
        }
    }


    override fun <T : Serializable> saveCacheListData(list: List<T>) {
        if (list.isNotEmpty()) {
            when (list[0]) {
                is ProvincesVO -> {
                    CacheDiskUtils.getInstance().put(
                        AppConstants.CacheKey.CACHE_PROVINCES_DATA,
                        ProvincesDataCache(list as List<ProvincesVO>),
                        AppConstants.CacheKey.CACHE_SAVE_TIME_SECONDS
                    )
                }
                is BannerVO -> {
                    CacheDiskUtils.getInstance().put(
                        AppConstants.CacheKey.CACHE_HOME_BANNER,
                        BannerListDataCache(list as List<BannerVO>),
                        AppConstants.CacheKey.CACHE_SAVE_TIME_SECONDS
                    )
                }
                is NoticeBean -> {
                    CacheDiskUtils.getInstance().put(
                        AppConstants.CacheKey.CACHE_NOTICE,
                        NoticeListDataCache(list as List<NoticeBean>),
                        AppConstants.CacheKey.CACHE_SAVE_TIME_SECONDS
                    )
                }
                is CommonApplyListVO -> {
                    CacheDiskUtils.getInstance().put(
                        AppConstants.CacheKey.CACHE_COMMON_APPLY,
                        CommonApplyListDataCache(list as List<CommonApplyListVO>),
                        AppConstants.CacheKey.CACHE_SAVE_TIME_SECONDS
                    )
                }
            }
        }
    }

    override fun <T : Serializable> getCacheListData(key: String): List<T>? {
        return when (val serializable = CacheDiskUtils.getInstance().getSerializable(key)) {
            is ProvincesDataCache? -> {
                serializable?.provincesList as List<T>?
            }
            is BannerListDataCache? -> {
                serializable?.bannerList as List<T>?
            }
            is NoticeListDataCache? -> {
                serializable?.noticeList as List<T>?
            }
            is CommonApplyListDataCache? -> {
                serializable?.commonApplyList as List<T>?
            }
            else -> emptyList()
        }
    }

    override fun saveFileBean(fileBeanDO: PipeFileBeanDO): Single<Long> {
        return appDB.pipeFileBeanDao().insert(fileBeanDO)
    }

    override fun savaFileBeanAll(fileBeanDOList: List<PipeFileBeanDO>): Single<List<Long>> {
        return appDB.pipeFileBeanDao().insert(fileBeanDOList)
    }

    override fun getFileBeanAll(fileType: Int): Single<List<PipeFileBeanDO>> {
        return appDB.pipeFileBeanDao().findAll("fileType", fileType)
    }

    override fun deleteFileBean(fileBeanDO: PipeFileBeanDO): Single<Int> {
        return appDB.pipeFileBeanDao().delete(fileBeanDO)
    }
}